package org.example.hotel.controller;

import jakarta.annotation.PostConstruct;
import org.example.hotel.dto.RoomStatusDTO;
import org.example.hotel.dto.TotalCostDTO;
import org.example.hotel.service.RoomControlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/room")
public class RoomController {

    @Autowired
    private RoomControlService roomControlService;

    @PostConstruct
    public void onStartup() {
        roomControlService.initRoomChargesOnStartup();
    }

    @PostMapping("/{roomId}/ac/start")
    public void startAc(@PathVariable int roomId) {
        roomControlService.startAc(roomId);
    }

    @PostMapping("/{roomId}/ac/stop")
    public void stopAc(@PathVariable int roomId) {
        roomControlService.stopAc(roomId);
    }

    @PostMapping("/{roomId}/ac/set-temp")
    public void setTemp(@PathVariable int roomId, @RequestParam double temp) {
        roomControlService.setTargetTemp(roomId, temp);
    }

    @PostMapping("/{roomId}/ac/set-speed")
    public void setSpeed(@PathVariable int roomId, @RequestParam String speed) {
        roomControlService.setFanSpeed(roomId, speed);
    }

    @PostMapping("/{roomId}/ac/set-mode")
    public void setMode(@PathVariable int roomId, @RequestParam String mode) {
        roomControlService.setWorkMode(roomId, mode);
    }

    @GetMapping("/{roomId}/status")
    public RoomStatusDTO getStatus(@PathVariable int roomId) {
        return roomControlService.getRoomStatus(roomId);
    }

    @GetMapping("/{roomId}/total-cost")
    public ResponseEntity<TotalCostDTO> getTotalCost(@PathVariable int roomId) {
        TotalCostDTO dto = roomControlService.getCurrentTotalCost(roomId);
        return ResponseEntity.ok(dto);
    }

}
